package com.kkb.dao.impl;

import com.kkb.bean.Student;
import com.kkb.dao.StudentDao;
import com.kkb.enumeration.Sex;
import com.kkb.enumeration.StudentState;
import com.kkb.pojo.Page;
import com.kkb.util.DBUtils;
import com.kkb.util.EnumUtils;
import com.kkb.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 学生dao实现
 */
public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> getAll(Student student, Page page) {
        List<Student> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from t_student where state != "+StudentState.DELETE.getValue());
        List<Object> params = new ArrayList<>();
        // 姓名
        if (StringUtils.nonBlank(student.getName())) {
            sb.append(" and name like ?");
            params.add("%" + student.getName() + "%");
        }
        // 学号
        if (StringUtils.nonBlank(student.getNumber())) {
            sb.append(" and number like ?");
            params.add("%" + student.getNumber() + "%");
        }
        // 性别
        if (Objects.nonNull(student.getSex())) {
            sb.append(" and sex = ?");
            params.add(student.getSex().getValue());
        }
        // 分页
        sb.append(" limit ").append(page.getPageFirst()).append(",").append(page.getPageSize());

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtils.getConnection();
            ps = conn.prepareStatement(sb.toString());
            DBUtils.setParam(ps, params);
            rs = ps.executeQuery();
            while (rs.next()) {
                Student stu = new Student();
                stu.setId(rs.getInt("id"));
                stu.setNumber(rs.getString("number"));
                stu.setName(rs.getString("name"));
                stu.setSex(EnumUtils.getEnumByValue(Sex.class, rs.getInt("sex")));
                stu.setPhone(rs.getString("phone"));
                stu.setProfession(rs.getString("profession"));
                stu.setRegDate(rs.getDate("regdate"));
                list.add(stu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtils.close(conn, ps, rs);
        }
        return list;
    }

    @Override
    public int count(Student student) {
        StringBuilder sb = new StringBuilder("select count(1) from t_student where state != "+StudentState.DELETE.getValue());
        List<Object> params = new ArrayList<>();
        // 姓名
        if (StringUtils.nonBlank(student.getName())) {
            sb.append(" and name like ?");
            params.add("%" + student.getName() + "%");
        }
        // 学号
        if (StringUtils.nonBlank(student.getNumber())) {
            sb.append(" and number like ?");
            params.add("%" + student.getNumber() + "%");
        }
        // 性别
        if (Objects.nonNull(student.getSex())) {
            sb.append(" and sex = ?");
            params.add(student.getSex().getValue());
        }
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtils.getConnection();
            ps = conn.prepareStatement(sb.toString());
            DBUtils.setParam(ps, params);
            rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtils.close(conn, ps, rs);
        }
        return 0;
    }

    @Override
    public Student getById(Integer id) {
        StringBuilder sb = new StringBuilder("select * from t_student where id = ?");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Student stu = null;
        try {
            conn = DBUtils.getConnection();
            ps = conn.prepareStatement(sb.toString());
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                stu = new Student();
                stu.setId(rs.getInt("id"));
                stu.setName(rs.getString("name"));
                stu.setNumber(rs.getString("number"));
                stu.setSex(EnumUtils.getEnumByValue(Sex.class, rs.getInt("sex")));
                stu.setPhone(rs.getString("phone"));
                stu.setEmail(rs.getString("email"));
                stu.setRegistered(rs.getString("registered"));
                stu.setAddress(rs.getString("address"));
                stu.setProfession(rs.getString("profession"));
                stu.setIdNo(rs.getString("idnumber"));
                stu.setPolitics(rs.getString("politics"));
                stu.setRegDate(rs.getDate("regdate"));
                stu.setState(EnumUtils.getEnumByValue(StudentState.class, rs.getInt("state")));
                stu.setIntroduction(rs.getString("introduction"));
                stu.setGradeId(rs.getInt("gradeid"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtils.close(conn, ps, rs);
        }
        return stu;
    }

    @Override
    public int insert(Student stu) {
        String sql = "insert into t_student(id,name,number,sex,phone,email,registered,address,profession,idnumber,politics,regdate,state,introduction,gradeid)" +
                " values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        List<Object> params = new ArrayList<>();
        params.add(stu.getName());
        params.add(stu.getNumber());
        params.add(stu.getSex().getValue());
        params.add(stu.getPhone());
        params.add(stu.getEmail());
        params.add(stu.getRegistered());
        params.add(stu.getAddress());
        params.add(stu.getProfession());
        params.add(stu.getIdNo());
        params.add(stu.getPolitics());
        params.add(stu.getRegDate());
        params.add(stu.getState().getValue());
        params.add(stu.getIntroduction());
        params.add(stu.getGradeId());
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtils.getConnection();
            ps = conn.prepareStatement(sql);
            DBUtils.setParam(ps, params);
            return ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtils.close(conn, ps, null);
        }
        return 0;
    }

    @Override
    public int update(Student stu) {
        String sql = "update t_student set name = ?,number=?,sex=?,phone=?,email=?,registered=?,address=?,profession=?,idnumber=?,politics=?,introduction=?,gradeid=? where id = ?";
        List<Object> params = new ArrayList<>();
        params.add(stu.getName());
        params.add(stu.getNumber());
        params.add(stu.getSex().getValue());
        params.add(stu.getPhone());
        params.add(stu.getEmail());
        params.add(stu.getRegistered());
        params.add(stu.getAddress());
        params.add(stu.getProfession());
        params.add(stu.getIdNo());
        params.add(stu.getPolitics());
        params.add(stu.getIntroduction());
        params.add(stu.getGradeId());
        params.add(stu.getId());
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtils.getConnection();
            ps = conn.prepareStatement(sql);
            DBUtils.setParam(ps, params);
            return ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtils.close(conn, ps, null);
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        String sql = "update t_student set state = ? where id = ?";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, StudentState.DELETE.getValue());
            ps.setInt(2, id);
            return ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtils.close(conn, ps, null);
        }
        return 0;
    }

    @Override
    public boolean checkNumber(String number, Integer id) {
        String sql = "select count(1) from t_student where number = ?";
        List<Object> params = new ArrayList<>();
        params.add(number);
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        if (id != null){
            sql += " and id != ?";
            params.add(id);
        }
        try {
            conn = DBUtils.getConnection();
            ps = conn.prepareStatement(sql);
            DBUtils.setParam(ps,params);
            rs = ps.executeQuery();
            if (rs.next()){
                if (rs.getInt(1) > 0){
                    return true;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtils.close(conn, ps, rs);
        }
        return false;
    }
}
